﻿using System;
using System.Collections.Generic;
using System.Text;
using Graffiti.Core;
using RssToolkit.Rss;
using System.Collections.Specialized;
using System.Web;
using System.Xml;

namespace GraffitiExtras.Plugins.FeedAds
{
    public class FeedAds : GraffitiEvent
    {
        public string ADTEXT;

        private const int textboxLines = 10;

        public override string Name
        {
            get
            {
                return "Feed Ads";
            }
        }

        public override string Description
        {
            get
            {
                return
                    "Plug-in to add advertisment code to feed items. " +
                    "Done by <a href=\"http://nayyeri.net\">Keyvan Nayyeri</a>.";
            }
        }

        public override bool IsEditable
        {
            get
            {
                return true;
            }
        }

        protected override NameValueCollection DataAsNameValueCollection()
        {
            NameValueCollection nvc = base.DataAsNameValueCollection();
            nvc["ADTEXT"] = ADTEXT;

            return nvc;
        }

        public override StatusType SetValues(HttpContext context, NameValueCollection nvc)
        {
            base.SetValues(context, nvc);
            ADTEXT = nvc["ADTEXT"];
            return StatusType.Success;
        }

        protected override FormElementCollection AddFormElements()
        {
            FormElementCollection fec = new FormElementCollection();

            fec.Add(new TextAreaFormElement("ADTEXT", "Ads Code",
                "Advertisment code", textboxLines));

            return fec;
        }

        public override void Init(GraffitiApplication ga)
        {
            ga.RssItem += new RssPostEventHandler(ga_RssItem);
        }

        void ga_RssItem(XmlTextWriter writer, PostEventArgs e)
        {
            e.Post.ExtendedBody += ADTEXT;
        }
    }
}
